#include "wifi_driver.h"

static const char *TAG = "wifi_driver";
bool wifi_ip_got_state;
wifi_mode_t selectMode = WIFI_MODE_STA;

extern void wifi_event_STA_GOT_IP_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
extern void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);

/**
 * @brief wifi初始化
 *
 * @param mode  模式
 * @param useBeforeSTA      是否使用flash存储的sta数据
 */
void wifi_init(wifi_mode_t mode, bool useBeforeSTA)
{
    selectMode = mode;

    if (useBeforeSTA == false)
    {
        // ESP_ERROR_CHECK(nvs_flash_erase());
        // ESP_ERROR_CHECK(nvs_flash_init());
        ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    }

    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    switch (mode)
    {
    case WIFI_MODE_STA:
        esp_netif_create_default_wifi_sta();
        break;
    case WIFI_MODE_AP:
        esp_netif_create_default_wifi_ap();
        break;
    case WIFI_MODE_APSTA:
        esp_netif_create_default_wifi_sta();
        esp_netif_create_default_wifi_ap();
        break;
    default:
        break;
    }

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &wifi_event_handler,
                                                        NULL,
                                                        NULL));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &wifi_event_STA_GOT_IP_handler,
                                                        NULL,
                                                        NULL));

    wifi_config_t ap_config = {
        .ap = {
            .ssid = AP_SSID,
            .ssid_len = strlen(AP_SSID),
            .channel = 1,
            .password = AP_PASSWORD,
            .max_connection = 4,
            .authmode = WIFI_AUTH_WPA2_PSK,
            // .pmf_cfg = {
            //     .required = true,
            // },
        },
    };

    ESP_ERROR_CHECK(esp_wifi_set_mode(mode));
    if (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA)
    {
        esp_wifi_set_config(WIFI_IF_AP, &ap_config);
    }
    ESP_ERROR_CHECK(esp_wifi_start());
}

void wifi_connect(uint8_t *ssid, uint8_t *password)
{
    wifi_config_t sta_config = {
        .sta = {
            .ssid = "",
            .password = "",
            .scan_method = WIFI_FAST_SCAN,
            .sort_method = WIFI_CONNECT_AP_BY_SECURITY,
            .threshold = {
                .rssi = -127,
                .authmode = WIFI_AUTH_OPEN,
            },
        },
    };
    // 复制 ssid
    strncpy((char *)sta_config.sta.ssid, (const char *)ssid, sizeof(sta_config.sta.ssid) - 1);

    // 复制 password
    strncpy((char *)sta_config.sta.password, (const char *)password, sizeof(sta_config.sta.password) - 1);

    ESP_ERROR_CHECK(esp_wifi_set_mode(selectMode));

    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));

    ESP_ERROR_CHECK(esp_wifi_connect());
}

void wifi_stop(void)
{
    esp_wifi_stop();
}
